<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
            	            	        <el-form-item
      	        label="用户id"
              >
      	<el-input v-model="searchFormData.userId" :clearable="true" placeholder="用户id" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="用户名"
              >
      	<el-input v-model="searchFormData.username" :clearable="true" placeholder="用户名" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="订单名称(订单名称连接字符)，超过长度则以...结尾"
              >
      	<el-input v-model="searchFormData.orderName" :clearable="true" placeholder="订单名称(订单名称连接字符)，超过长度则以...结尾" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="封面图片地址"
              >
      	<el-input v-model="searchFormData.imgUrl" :clearable="true" placeholder="封面图片地址" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="预支付交易会话标识，用于后续接口调用中使用，该值有效期为2小时"
              >
      	<el-input v-model="searchFormData.prepayId" :clearable="true" placeholder="预支付交易会话标识，用于后续接口调用中使用，该值有效期为2小时" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="订单编号"
              >
      	<el-input v-model="searchFormData.orderNo" :clearable="true" placeholder="订单编号" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="收货人"
              >
      	<el-input v-model="searchFormData.receiverName" :clearable="true" placeholder="收货人" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="收货电话"
              >
      	<el-input v-model="searchFormData.receiverPhone" :clearable="true" placeholder="收货电话" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="收货地址"
              >
      	<el-input v-model="searchFormData.receiverAddress" :clearable="true" placeholder="收货地址" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="配送地址"
              >
      	<el-input v-model="searchFormData.shippingAddress" :clearable="true" placeholder="配送地址" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="配送地联系方式"
              >
      	<el-input v-model="searchFormData.shippingPhone" :clearable="true" placeholder="配送地联系方式" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="商品数量"
              >
      	<el-input v-model="searchFormData.contentNum" :clearable="true" placeholder="商品数量" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="商品销售总价"
              >
      	<el-input v-model="searchFormData.totalPrice" :clearable="true" placeholder="商品销售总价" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="物流单号"
              >
      	<el-input v-model="searchFormData.logisticsNo" :clearable="true" placeholder="物流单号" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="支付方式：1=在线支付，2=货到付款"
              >
      	<el-input v-model="searchFormData.payChannel" :clearable="true" placeholder="支付方式：1=在线支付，2=货到付款" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="订单状态: 0=待支付，1=已支付，待发货, 2=已发货/待收货，3=确认收货/已完成，4=用户关闭，5=平台关闭(商家)，6=系统调度关闭"
              >
      	<el-input v-model="searchFormData.orderState" :clearable="true" placeholder="订单状态: 0=待支付，1=已支付，待发货, 2=已发货/待收货，3=确认收货/已完成，4=用户关闭，5=平台关闭(商家)，6=系统调度关闭" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="支付时间"
              >
      	<el-input v-model="searchFormData.payTime" :clearable="true" placeholder="支付时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="运费"
              >
      	<el-input v-model="searchFormData.freight" :clearable="true" placeholder="运费" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="是否启用，1表示启用，0表示禁用"
              >
      	<el-input v-model="searchFormData.enabled" :clearable="true" placeholder="是否启用，1表示启用，0表示禁用" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建时间"
              >
      	<el-input v-model="searchFormData.createTime" :clearable="true" placeholder="数据创建时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据最后修改时间"
              >
      	<el-input v-model="searchFormData.updateTime" :clearable="true" placeholder="数据最后修改时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建者"
              >
      	<el-input v-model="searchFormData.creator" :clearable="true" placeholder="数据创建者" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据更新者"
              >
      	<el-input v-model="searchFormData.updater" :clearable="true" placeholder="数据更新者" style="width: 250px;" />
      </el-form-item>
                    <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">新增</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
          <el-table-column
        prop="id"
                label="id"
              />
           <el-table-column
        prop="userId"
                label="用户id"
              />
           <el-table-column
        prop="username"
                label="用户名"
              />
           <el-table-column
        prop="orderName"
                label="订单名称(订单名称连接字符)，超过长度则以...结尾"
              />
           <el-table-column
        prop="imgUrl"
                label="封面图片地址"
              />
           <el-table-column
        prop="prepayId"
                label="预支付交易会话标识，用于后续接口调用中使用，该值有效期为2小时"
              />
           <el-table-column
        prop="orderNo"
                label="订单编号"
              />
           <el-table-column
        prop="receiverName"
                label="收货人"
              />
           <el-table-column
        prop="receiverPhone"
                label="收货电话"
              />
           <el-table-column
        prop="receiverAddress"
                label="收货地址"
              />
           <el-table-column
        prop="shippingAddress"
                label="配送地址"
              />
           <el-table-column
        prop="shippingPhone"
                label="配送地联系方式"
              />
           <el-table-column
        prop="contentNum"
                label="商品数量"
              />
           <el-table-column
        prop="totalPrice"
                label="商品销售总价"
              />
           <el-table-column
        prop="logisticsNo"
                label="物流单号"
              />
           <el-table-column
        prop="payChannel"
                label="支付方式：1=在线支付，2=货到付款"
              />
           <el-table-column
        prop="orderState"
                label="订单状态: 0=待支付，1=已支付，待发货, 2=已发货/待收货，3=确认收货/已完成，4=用户关闭，5=平台关闭(商家)，6=系统调度关闭"
              />
           <el-table-column
        prop="payTime"
                label="支付时间"
              />
           <el-table-column
        prop="freight"
                label="运费"
              />
           <el-table-column
        prop="enabled"
                label="是否启用，1表示启用，0表示禁用"
              />
           <el-table-column
        prop="createTime"
                label="数据创建时间"
              />
           <el-table-column
        prop="updateTime"
                label="数据最后修改时间"
              />
           <el-table-column
        prop="creator"
                label="数据创建者"
              />
           <el-table-column
        prop="updater"
                label="数据更新者"
              />
          <el-table-column>
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm('dialogForm')"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"
        size="mini"
      >
            	            	        <el-form-item
      	prop="userId"
                label="用户id"
               >
          <el-input v-model="dialogFormData.userId" />
        </el-form-item>
                    	        <el-form-item
      	prop="username"
                label="用户名"
               >
          <el-input v-model="dialogFormData.username" />
        </el-form-item>
                    	        <el-form-item
      	prop="orderName"
                label="订单名称(订单名称连接字符)，超过长度则以...结尾"
               >
          <el-input v-model="dialogFormData.orderName" />
        </el-form-item>
                    	        <el-form-item
      	prop="imgUrl"
                label="封面图片地址"
               >
          <el-input v-model="dialogFormData.imgUrl" />
        </el-form-item>
                    	        <el-form-item
      	prop="prepayId"
                label="预支付交易会话标识，用于后续接口调用中使用，该值有效期为2小时"
               >
          <el-input v-model="dialogFormData.prepayId" />
        </el-form-item>
                    	        <el-form-item
      	prop="orderNo"
                label="订单编号"
               >
          <el-input v-model="dialogFormData.orderNo" />
        </el-form-item>
                    	        <el-form-item
      	prop="receiverName"
                label="收货人"
               >
          <el-input v-model="dialogFormData.receiverName" />
        </el-form-item>
                    	        <el-form-item
      	prop="receiverPhone"
                label="收货电话"
               >
          <el-input v-model="dialogFormData.receiverPhone" />
        </el-form-item>
                    	        <el-form-item
      	prop="receiverAddress"
                label="收货地址"
               >
          <el-input v-model="dialogFormData.receiverAddress" />
        </el-form-item>
                    	        <el-form-item
      	prop="shippingAddress"
                label="配送地址"
               >
          <el-input v-model="dialogFormData.shippingAddress" />
        </el-form-item>
                    	        <el-form-item
      	prop="shippingPhone"
                label="配送地联系方式"
               >
          <el-input v-model="dialogFormData.shippingPhone" />
        </el-form-item>
                    	        <el-form-item
      	prop="contentNum"
                label="商品数量"
               >
          <el-input v-model="dialogFormData.contentNum" />
        </el-form-item>
                    	        <el-form-item
      	prop="totalPrice"
                label="商品销售总价"
               >
          <el-input v-model="dialogFormData.totalPrice" />
        </el-form-item>
                    	        <el-form-item
      	prop="logisticsNo"
                label="物流单号"
               >
          <el-input v-model="dialogFormData.logisticsNo" />
        </el-form-item>
                    	        <el-form-item
      	prop="payChannel"
                label="支付方式：1=在线支付，2=货到付款"
               >
          <el-input v-model="dialogFormData.payChannel" />
        </el-form-item>
                    	        <el-form-item
      	prop="orderState"
                label="订单状态: 0=待支付，1=已支付，待发货, 2=已发货/待收货，3=确认收货/已完成，4=用户关闭，5=平台关闭(商家)，6=系统调度关闭"
               >
          <el-input v-model="dialogFormData.orderState" />
        </el-form-item>
                    	        <el-form-item
      	prop="payTime"
                label="支付时间"
               >
          <el-input v-model="dialogFormData.payTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="freight"
                label="运费"
               >
          <el-input v-model="dialogFormData.freight" />
        </el-form-item>
                    	        <el-form-item
      	prop="enabled"
                label="是否启用，1表示启用，0表示禁用"
               >
          <el-input v-model="dialogFormData.enabled" />
        </el-form-item>
                    	        <el-form-item
      	prop="createTime"
                label="数据创建时间"
               >
          <el-input v-model="dialogFormData.createTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="updateTime"
                label="数据最后修改时间"
               >
          <el-input v-model="dialogFormData.updateTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="creator"
                label="数据创建者"
               >
          <el-input v-model="dialogFormData.creator" />
        </el-form-item>
                    	        <el-form-item
      	prop="updater"
                label="数据更新者"
               >
          <el-input v-model="dialogFormData.updater" />
        </el-form-item>
                    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
// 创建axios实例
const client = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
  timeout: 60000 // 请求超时时间,60秒
})
export default {
  data() {    
    return {
      searchFormData: {
                id: '',
                userId: '',
                username: '',
                orderName: '',
                imgUrl: '',
                prepayId: '',
                orderNo: '',
                receiverName: '',
                receiverPhone: '',
                receiverAddress: '',
                shippingAddress: '',
                shippingPhone: '',
                contentNum: '',
                totalPrice: '',
                logisticsNo: '',
                payChannel: '',
                orderState: '',
                payTime: '',
                freight: '',
                enabled: '',
                createTime: '',
                updateTime: '',
                creator: '',
                updater: '',
                pageIndex: 1,
        pageSize: 10
      },
      pageInfo: {
        rows: [],
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogFormData: {
                 id: ''
                , userId: ''
                , username: ''
                , orderName: ''
                , imgUrl: ''
                , prepayId: ''
                , orderNo: ''
                , receiverName: ''
                , receiverPhone: ''
                , receiverAddress: ''
                , shippingAddress: ''
                , shippingPhone: ''
                , contentNum: ''
                , totalPrice: ''
                , logisticsNo: ''
                , payChannel: ''
                , orderState: ''
                , payTime: ''
                , freight: ''
                , enabled: ''
                , createTime: ''
                , updateTime: ''
                , creator: ''
                , updater: ''
              },
      dialogFormRules: {        
                 id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , userId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , username: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , orderName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , imgUrl: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , prepayId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , orderNo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , receiverName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , receiverPhone: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , receiverAddress: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , shippingAddress: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , shippingPhone: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , contentNum: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , totalPrice: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , logisticsNo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , payChannel: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , orderState: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , payTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , freight: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , enabled: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , creator: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updater: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
              }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.post('/orders/list', this.searchFormData, resp => {
        this.pageInfo.rows = resp.data
      })
    },
    onTableUpdate(row) {
      this.dialogTitle = '修改'
      this.dialogVisible = true
      this.$nextTick(() => {
        Object.assign(this.dialogFormData, row)
      })
    },
    onTableDelete(row) {
      this.confirm(`确认要删除该记录吗？`, function(done) {
        const data = {
          id: row.id
        }
        this.post('/orders/del', data, () => {
          done()
          this.tip('删除成功')
          this.loadTable()
        })
      })
    },
    onDialogSave() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          const uri = this.dialogFormData.id ? '/orders/update' : '/orders/add'
          this.post(uri, this.dialogFormData, () => {
            this.dialogVisible = false
            this.loadTable()
          })
        }
      })
    },
    onSizeChange(size) {
      this.searchFormData.pageSize = size
      this.loadTable()
    },
    onAdd() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
      this.dialogFormData.id = 0
    },
    onPageIndexChange(pageIndex) {
      this.searchFormData.pageIndex = pageIndex
      this.loadTable()
    },
    /**
     * 请求接口
     * @param uri uri
     * @param data 请求数据
     * @param callback 成功时回调
     * @param errorCallback 错误时回调
     */
    post(uri, data, callback, errorCallback) {
      const that = this
      client.post(uri, data).then(function(response) {
        const resp = response.data
        const code = resp.code
        if (code === '0') { // 成功
          callback && callback.call(that, resp)
        } else {
          that.$message.error(resp.msg)
        }
      }).catch(function(error) {
        console.error(error)
        errorCallback && errorCallback(error)
        that.$message.error(error.message)
      })
    }
  }
}
</script>